﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Export.Ui.ViewModel
{
    using DatabaseCore;
    using FirstFloor.ModernUI.Presentation;
    using GalaSoft.MvvmLight;
    using System.Windows.Controls;

    public class LoginViewModel : ViewModelBase
    {
        private MainWindowViewModel _mainWindowViewModel;
        private UserContext _currentUser;
        
        private IUserDao _userDao;

        public LoginViewModel()
        {
            LoginCommand = new RelayCommand(Login);
        }

        public LoginViewModel(MainWindowViewModel mainWindowViewModel, IUserDao userDao)
        {
            _mainWindowViewModel = mainWindowViewModel;
            _userDao = userDao;
            LoginCommand = new RelayCommand(Login);
        }

        public string Username{ get; set; }

        public RelayCommand LoginCommand { get; private set; }

        public UserContext getUserContext()
        {
            return _currentUser;
        }

        private void Login(object parameter)
        {
            PasswordBox Password = new PasswordBox();
            if (parameter != null)
            {
                Password = (PasswordBox)parameter;
            }
            var user = _userDao.Login(Username, Password.Password);
            if (user != null)
            {
                _currentUser = new UserContext(user);
                _mainWindowViewModel.UpdateMenuSelection();
            }
        }
    }
}
